package com.wangyi.im.remoting;

import com.alibaba.fastjson.JSONObject;
import com.wangyi.im.execute.IMMessageExecute;
import com.wangyi.im.request.*;
import com.wangyi.im.response.IMMessageQueryResponse;
import com.wangyi.im.response.IMMessageResponse;
import com.wangyi.im.response.IMResponse;
import com.wangyi.im.utils.BuildDataUtil;
import retrofit2.Call;
import retrofit2.Response;

import java.io.IOException;
import java.util.List;

import static com.wangyi.im.constants.CommonConstants.INFO;
import static com.wangyi.im.constants.CommonConstants.MSGS;

public class IMMessageRemoting extends Remoting {

    public IMMessageRemoting(String baseUrl, String appKey, String appSecret) {
        super(baseUrl, appKey, appSecret);
    }

    public IMResponse<IMMessageResponse> sendMsg(IMMessageRequest imMessageRequest){
        Call<JSONObject> call = getRetrofit(imMessageRequest).create(IMMessageExecute.class).sendMsg();
        Response<JSONObject> retrofitResponse = null;
        try {
            retrofitResponse = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new BuildDataUtil().buildData(retrofitResponse, IMMessageResponse.class, INFO);
    }

    public IMResponse<IMMessageResponse> sendBatchMsg(IMBatchMessageRequest imBatchMessageRequest){
        Call<JSONObject> call = getRetrofit(imBatchMessageRequest).create(IMMessageExecute.class).sendBatchMsg();
        Response<JSONObject> retrofitResponse = null;
        try {
            retrofitResponse = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new BuildDataUtil().buildData(retrofitResponse, IMMessageResponse.class, INFO);
    }

    public IMResponse<IMMessageResponse> sendAttachMsg(IMAttachMessageRequest imAttachMessageRequest){
        Call<JSONObject> call = getRetrofit(imAttachMessageRequest).create(IMMessageExecute.class).sendAttachMsg();
        Response<JSONObject> retrofitResponse = null;
        try {
            retrofitResponse = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new BuildDataUtil().buildData(retrofitResponse, IMMessageResponse.class, INFO);
    }

    public IMResponse<IMMessageResponse> sendBatchAttachMsg(IMBatchAttachMessageRequest imBatchAttachMessageRequest){
        Call<JSONObject> call = getRetrofit(imBatchAttachMessageRequest).create(IMMessageExecute.class).sendBatchAttachMsg();
        Response<JSONObject> retrofitResponse = null;
        try {
            retrofitResponse = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new BuildDataUtil().buildData(retrofitResponse, IMMessageResponse.class, INFO);
    }

    public IMResponse<IMMessageResponse> recall(IMMessageRequest imMessageRequest){
        Call<JSONObject> call = getRetrofit(imMessageRequest).create(IMMessageExecute.class).sendMsg();
        Response<JSONObject> retrofitResponse = null;
        try {
            retrofitResponse = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new BuildDataUtil().buildData(retrofitResponse, IMMessageResponse.class, INFO);
    }

    public IMResponse<IMMessageResponse> broadcastMsg(IMBroadcastMessageRequest imBroadcastMessageRequest){
        Call<JSONObject> call = getRetrofit(imBroadcastMessageRequest).create(IMMessageExecute.class).broadcastMsg();
        Response<JSONObject> retrofitResponse = null;
        try {
            retrofitResponse = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new BuildDataUtil().buildData(retrofitResponse, IMMessageResponse.class, INFO);
    }

    public IMResponse<IMMessageResponse> delMsgOneWay(IMMessageRequest imMessageRequest){
        Call<JSONObject> call = getRetrofit(imMessageRequest).create(IMMessageExecute.class).delMsgOneWay();
        Response<JSONObject> retrofitResponse = null;
        try {
            retrofitResponse = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new BuildDataUtil().buildData(retrofitResponse, IMMessageResponse.class, INFO);
    }

    public IMResponse<IMMessageResponse> delRoamSession(IMMessageRequest imMessageRequest){
        Call<JSONObject> call = getRetrofit(imMessageRequest).create(IMMessageExecute.class).delRoamSession();
        Response<JSONObject> retrofitResponse = null;
        try {
            retrofitResponse = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new BuildDataUtil().buildData(retrofitResponse, IMMessageResponse.class, INFO);
    }

    public IMResponse<List<IMMessageQueryResponse>> querySessionMsg(IMMessageQueryRequest imMessageQueryRequest){

        Call<JSONObject> call = getRetrofit(imMessageQueryRequest).create(IMMessageExecute.class).querySessionMsg();
        Response<JSONObject> retrofitResponse = null;
        try {
            retrofitResponse = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new BuildDataUtil().buildListData(retrofitResponse, IMMessageQueryResponse.class, MSGS);
    }
}
